#include <jni.h>
#include <android/log.h>
#include "utilities.h"


#define LOG_TAG "FaceDetection/DetectionBasedTracker"
#define LOGD(...) ((void)__android_log_print(ANDROID_LOG_DEBUG, LOG_TAG, __VA_ARGS__))

//#include <vector>

using namespace std;
using namespace cv;

int *res = NULL;
//Java_com_example_android_mediafx_HelloEffects_calculatePoints
//Java_com_scanner_JniWrapper_calculatePoints
extern "C" {
JNIEXPORT jintArray JNICALL Java_com_scanner_JniWrapper_calculatePoints(JNIEnv* env, jobject,jbyteArray imageData, jint height, jint width,jint optionEQ, jint optionMask)
{
	jbyte* _in = env->GetByteArrayElements(imageData, 0);
	jintArray Edges = env->NewIntArray(8);	
	jint *narr = env->GetIntArrayElements(Edges, NULL);
	//Build the Mat structure for input data
	//Mat mSrc(height, width, CV_8UC4, (unsigned char *)_in);
	
	//jbyte * pNV21FrameData = env->GetByteArrayElements(imageData, 0);
	//jint * poutPixels = env->GetIntArrayElements(outPixels, 0);
	LOGD("xxx wrapper before nyuv");	
	Mat BGR;
	Mat myuv(height + height/2, width, CV_8UC1, (unsigned char *)_in);
	//Mat mResult(height*RESIZE_IMAGE, width*RESIZE_IMAGE, CV_8UC4, (unsigned char *)poutPixels);
	LOGD("xxx wrapper before convert");	
	cvtColor(myuv, BGR, CV_YUV420sp2BGRA);
	LOGD("xxx wrapper after convert");	
	
	
	res = GetRectCorners(BGR, optionEQ, optionMask);
	if (res[0] == -1)
	//did not succeed
	{	
		narr[0] = -1;
	}
	else
	{
		for (int ind = 0 ; ind<8;ind++)
		{
			narr[ind] = res[ind];
		}
	}

	env->ReleaseByteArrayElements(imageData, _in, 0);
	env->ReleaseIntArrayElements(Edges, narr, 0);
	return Edges;
}

/*
JNIEXPORT jintArray JNICALL Java_com_scanner_JniWrapper_getCanny(JNIEnv* env, jobject,jintArray imageData, jint height, jint width)
{
	jint* _in = env->GetIntArrayElements(imageData, 0);
    LOGD("xxx _in defined");
	//Build the Mat structure for input data
	Mat mSrc(height, width, CV_8UC4, (unsigned char *)_in);
    LOGD("xxx Mat created");
	generateCanny(mSrc);
	
	int picSize = height*width;
	
	jintArray outArr = env->NewIntArray(picSize);
	if (outArr == NULL)
	{
		return NULL;
	}
	jint *outImage = env->GetIntArrayElements(outArr,NULL);
	
	if (outImage == NULL)
	{
		return outArr;
	}
	
	for (int i =0; i < picSize; i++)
	{
		outImage[i] = mSrc.data[i];
	}
	
	//release the pointer.
	env->ReleaseIntArrayElements(imageData, _in, 0);
	env->ReleaseIntArrayElements(outArr, outImage, 0);
	return outArr;
}*/

}
